package com.topgay.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.topgay.domain.Member;
import com.topgay.util.BoardUtilities;

@Repository
public class MemberDAOImple implements MemberDAO{

	@Autowired
	private SqlSessionTemplate session;
	
	@Override
	public Member selectMemberById(String id) throws SQLException {
		return session.selectOne("member.selectMemberById", id);
	}

	@Override
	public Member selectMemberByNickname(String nickname) throws SQLException {
		return session.selectOne("member.selectMemberByNickname", nickname);
	}
	@Override
	public int updateLoginCheck(String id) throws SQLException {
		return session.update("member.updateLoginCheck", id);
	};
	@Override
	public int updateLogoutCheck(String id) throws SQLException {
		System.out.println(id);
		return session.update("member.updateLogoutCheck", id);
	};
	@Override
	public int insertMember(Member member) throws SQLException {
		System.out.println(member);
		return session.insert("member.insertMember", member);
	}
	@Override
	public int updateMember(Member member) throws SQLException {
		return session.update("member.updateMember", member);
	}
	@Override
	public int deleteMemberById(String id) throws SQLException {
		System.out.println("id = "+id);
		return session.delete("member.deleteMemberById", id);
	}
	public Member findMemberById(String name, String hp) throws SQLException{
		HashMap map = new HashMap<String, String>();
		map.put("name", name);
		map.put("hp", hp);
		
		return session.selectOne("member.findMemberById", map);
	}
	public Member findMemberByPw(String id, String name, String hp) throws SQLException{
		HashMap map = new HashMap<String, String>();
		map.put("id", id);
		map.put("name", name);
		map.put("hp", hp);
		return session.selectOne("member.findMemberByPw", map);
		
	}
	
	@Override
	public List<Member> selectMemberList(int page, String role) {
		HashMap map = new HashMap();
		map.put("contentPerPage", BoardUtilities.CONTENT_PER_PAGE);
		map.put("page", page);
		map.put("role", role);
		return session.selectList(makeSqlId("selectMemberList"), map);
	}

	@Override
	public int selectTotalContent(String role) {
		return session.selectOne(makeSqlId("selectTotalContent"), role);
	}
	
	@Override
	public Member selectMemberContentById(String id) {
		return session.selectOne(makeSqlId("selectMemberContentById"), id);
	}
	

	private String makeSqlId(String id){
		return "member." + id;
	}
	
}
